package cn.stazxr.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Configuration;

import java.io.Serial;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
 * @author talentestors
 * @version 1.0
 * @since 2025.10.27
 **/
@Configuration
public class CustomObjectMapper extends ObjectMapper {
	@Serial
	private static final long serialVersionUID = 4131252348334144628L;

	public CustomObjectMapper() {
		super();
		//关键：注册 Java 8 时间模块
		registerModule(new JavaTimeModule());
		//去掉默认的时间戳格式
		configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
		//设置为东八区
		setTimeZone(TimeZone.getTimeZone("GMT+8"));
		//设置日期转换yyyy-MM-dd HH:mm:ss
		setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		// 空值不序列化
		setSerializationInclusion(JsonInclude.Include.NON_NULL);
		// 反序列化时，属性不存在的兼容处理
//		getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		// 序列化枚举是以toString()来输出，默认false，即默认以name()来输出
		configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
	}
}
